package com.nb.controller;

import com.nb.dto.AssignAclDTO;
import com.nb.dto.AssignDataScopeDTO;
import com.nb.dto.AssignRolesDTO;
import com.nb.result.Result;
import com.nb.service.AuthService;
import com.nb.service.RoleService;
import com.nb.service.UserService;
import com.nb.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private UserService userService;
    //获取当前登录用户信息（分配菜单）
    @Autowired
    private RoleService roleService;
    @GetMapping("/info")
    public Result getAuthInfo() {
        UserInfoVO userInfoVO = authService.getAuthInfo();
        return Result.success(userInfoVO);
    }
    /**
     * 用户角色分配
     * @param dto 用户ID和角色ID列表
     * @return 操作结果
     */
    @PostMapping("/assignRoles")
    public Result assignRoles(@Validated @RequestBody AssignRolesDTO dto) {
        userService.assignRoles(dto);
        return Result.success();
    }
    /**
     * 角色权限分配
     * @param dto 角色ID和权限ID列表
     * @return 操作结果
     */
    @PostMapping("/assignAcls")
    public Result assignAcls(@Validated @RequestBody AssignAclDTO dto) {
        roleService.assignAcl(dto);
        return Result.success();
    }
    /**
     * 角色数据权限配置
     * @param dto 角色ID和部门ID列表
     * @return 操作结果
     */
    @PostMapping("/dataScope")
    public Result assignDataScope(@Validated @RequestBody AssignDataScopeDTO dto) {
        roleService.assignDataScope(dto);
        return Result.success();
    }
    /**
     * 用户权限查询：查询用户角色、角色权限点、返回权限树
     * @return 权限信息
     */
    @GetMapping("/userPermissions")
    public Result getUserPermissions() {
        Map<String, Object> permissions = authService.getUserPermissions();
        return Result.success(permissions);
    }
}
